package com.cxs.controller;

import com.cxs.entity.dto.GetRoomInfoDTO;
import com.cxs.enums.OpenTypeEnum;
import com.cxs.entity.Classroom;
import com.cxs.service.ClassroomService;
import com.cxs.service.SeatService;
import com.cxs.entity.vo.ClassroomVO;
import com.cxs.entity.vo.SeatInfoVO;
import com.cxs.entity.vo.SeatOrderInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/*
 * @Project:seat-subscribe-sys
 * @Author:cxs
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
@RequestMapping("/room")
public class ClassroomController {

    @Autowired
    private ClassroomService classroomService;

    @Autowired
    private SeatService seatService;

    @RequestMapping("/toRoomList")
    public String toAdminList(Model model, HttpSession session) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        List<Classroom> list = classroomService.list(null);

        List<ClassroomVO> voList = CollectionUtils.isEmpty(list) ? new ArrayList<>(0) : list.stream().map(r -> {
            ClassroomVO vo = new ClassroomVO();
            BeanUtils.copyProperties(r, vo);
            vo.setOpenTimeEndStr(formatter.format(r.getOpenTimeEnd()));
            vo.setOpenTimeStartStr(formatter.format(r.getOpenTimeStart()));
            vo.setOpenTimeRange(vo.getOpenTimeStartStr() + "-" + vo.getOpenTimeEndStr());
            vo.setOpenDesc(OpenTypeEnum.getDesc(r.getOpenType()));
            return vo;
        }).collect(Collectors.toList());
        model.addAttribute("roomList", voList);
        model.addAttribute("user", session.getAttribute("loginUser"));
        return "roomList";
    }


    @RequestMapping("/getRoomInfo")
    public String getRoomInfo(GetRoomInfoDTO dto, Model model, HttpSession session) {
        Integer id = dto.getId();
        if (null == id) {
            return "redirect:/404";
        }

        if (null == dto.getDate()) {
            dto.setDate(LocalDate.now());
        }

        Classroom classroom = classroomService.getById(id);
        if (null == classroom) {
            return "redirect:/404";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        SeatInfoVO info = classroomService.getSeatListInfo(classroom, dto.getDate());
        model.addAttribute("user", session.getAttribute("loginUser"));
        model.addAttribute("dateinfo", formatter.format(dto.getDate()));
        model.addAttribute("info", info);
        return "roomInfo";
    }

    @RequestMapping("/toSeatInfo")
    public String toSeatInfo(GetRoomInfoDTO dto, Model model, HttpSession session) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<SeatOrderInfoVO> infoList = seatService.getSeatListInfo(dto.getId(), dto.getDate());
        model.addAttribute("user", session.getAttribute("loginUser"));
        model.addAttribute("dateinfo", formatter.format(dto.getDate()));
        model.addAttribute("infoList", infoList);
        return "roomOrderInfo";
    }


}
